from fastapi import APIRouter, Depends, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_kylin.entity.vo.future_strategy_position_vo import (
    FutureStrategyPositionModel,
    FutureStrategyPositionPageQueryModel,
    DeleteFutureStrategyPositionModel,
)
from module_kylin.service.future_strategy_position_service import FutureStrategyPositionService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

# 构建路由
futureStrategyPositionController = APIRouter(prefix='/kylin/futureStrategyPosition', tags=['期货策略仓位管理'],
                                             dependencies=[Depends(LoginService.get_current_user)])


@futureStrategyPositionController.get('/list', response_model=PageResponseModel,
                                      dependencies=[
                                          Depends(CheckUserInterfaceAuth('kylin:futureStrategyPosition:list'))])
async def get_future_strategy_position_list(
        request: Request,
        position_page_query: FutureStrategyPositionPageQueryModel = Depends(
            FutureStrategyPositionPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
        is_page: bool = True
):
    """
    获取策略仓位列表
    """
    # 获取分页数据
    position_page_query_result = await FutureStrategyPositionService.get_strategy_position_list(
        query_db, position_page_query, is_page
    )
    logger.info('获取期货策略仓位列表成功')
    return ResponseUtil.success(model_content=position_page_query_result)


@futureStrategyPositionController.get('/{position_id}', response_model=FutureStrategyPositionModel,
                                      dependencies=[
                                          Depends(CheckUserInterfaceAuth('kylin:futureStrategyPosition:query'))])
async def query_detail_future_strategy_position(request: Request, position_id: int,
                                                query_db: AsyncSession = Depends(get_db)):
    """
    获取策略仓位详情
    """
    position_detail_result = await FutureStrategyPositionService.get_strategy_position_detail(
        query_db, position_id
    )
    logger.info(f'获取策略仓位{position_id}的信息成功')

    return ResponseUtil.success(data=position_detail_result)


@futureStrategyPositionController.post('', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:futureStrategyPosition:add'))])
@ValidateFields(validate_model='position')
@Log(title='期货策略仓位管理', business_type=BusinessType.INSERT)
async def add_future_strategy_position(
        request: Request,
        position: FutureStrategyPositionModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    新增策略仓位
    """
    add_position_result = await FutureStrategyPositionService.add_strategy_position(
        query_db, position
    )
    logger.info(f'用户{current_user.user.user_name}新增期货策略仓位成功')

    return ResponseUtil.success(data=add_position_result)


@futureStrategyPositionController.put('/{position_id}', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:futureStrategyPosition:edit'))])
@ValidateFields(validate_model='position')
@Log(title='期货策略仓位管理', business_type=BusinessType.UPDATE)
async def update_future_strategy_position(
        request: Request,
        position_id: int,
        position: FutureStrategyPositionModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    更新策略仓位
    """
    update_position_result = await FutureStrategyPositionService.update_strategy_position(
        query_db, position_id, position
    )
    logger.info(f'用户{current_user.user.user_name}更新期货策略仓位{position_id}成功')

    return ResponseUtil.success(data=update_position_result)


@futureStrategyPositionController.delete('', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:futureStrategyPosition:delete'))])
@Log(title='期货策略仓位管理', business_type=BusinessType.DELETE)
async def delete_future_strategy_position(
        request: Request,
        delete_model: DeleteFutureStrategyPositionModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    删除策略仓位
    """
    delete_position_result = await FutureStrategyPositionService.delete_strategy_position(
        query_db, delete_model
    )
    logger.info(f'用户{current_user.user.user_name}删除期货策略仓位成功')

    return ResponseUtil.success(msg=delete_position_result.message)
